package com.stone.util;

import com.stone.pojo.User;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stone on 17-5-22.
 * 抓取http://jinan.anjuke.com/tycoon/p2/网站经理人资料
 */
@Controller
public class WebPageSource {
    @RequestMapping(value = "catch")
    public String catchdc(Model model) {
        // public static void main(String args[]){


        Document doc = null;
       // Map<String,List<Map<String, String>>> mapAll = new HashMap<String,List<Map<String, String>>>();

        List<User> list = new ArrayList<User>();
        for (int i = 1; i <= 2; i++) {
           // System.out.println("当前页数:第" + i + "页");
            try {
                // 获取网页文本
                doc = Jsoup.connect("http://jinan.anjuke.com/tycoon/p" + i + "/").timeout(30000).get();

                // Elements elements = doc.getElementsByClass("jjr-title");
                Elements elements = doc.select("div.jjr-itemmod");

                for (int m = 0; m < elements.size(); m++) {
                    Element element = elements.get(m);
//                System.out.println("姓名:"+element.select("h3 a").attr("title"));
//                System.out.println("所属公司:"+element.select("p.mg-top").text());
//                System.out.println("熟悉领域:"+element.select("p.xq_tag").text());
//                System.out.println("电话:"+element.select("div.jjr-side").text());
                    User u = new User();
                    u.setName(element.select("h3 a").attr("title"));
                    u.setDept(element.select("p.mg-top").text());
                    u.setField(element.select("p.xq_tag").text());
                    u.setPhone(element.select("div.jjr-side").text());
                    list.add(u);
                   // mapAll.put("list",list);
                    // model.addAttribute("name",element.select("h3 a").attr("title"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(list);
        model.addAttribute("list", list);
       // model.addAttribute("mapAll", mapAll);
        return "catch";
    }



      /*  URL url;
        int responsecode;

        HttpURLConnection urlConnection;

        BufferedReader reader;

        String line;
        try{

            //生成一个URL对象，要获取源代码的网页地址为：http://www.baidu.com
            url=new URL("https://detail.tmall.com/item.htm?spm=a220m.1000862.1000725.31.NnWOGb&id=538199675058&is_b=1&cat_id=2&rn=675c758fbd489f537b27660627122217&sku_properties=10004:709990523;5919063:6536025");

            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();

            //设置 HttpURLConnection的字符编码
            urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
            //获取服务器响应代码
            responsecode=urlConnection.getResponseCode();

            if(responsecode==200){

                //得到输入流，即获得了网页的内容
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"gbk"));
                StringBuffer strBuffer = new StringBuffer();

                while((line=reader.readLine())!=null){

                    System.out.println(line.substring(0));

                }

            }

            else{

                System.out.println("获取不到网页的源码，服务器响应代码为："+responsecode);

            }

        }

        catch(Exception e){

            e.printStackTrace();
            System.out.println("获取不到网页的源码,出现异常："+e);

        }

    }*/


}

